<?php
namespace App\Http\Controllers\background;
use App\Http\Controllers\Controller;
use App\Http\Controllers\SendmsController;
use App\Models\Mysql\{XnMechantBalance, XnMerchant, XnOrder, XnSupplierGoods};
use Illuminate\Http\Request;
class MerchantApiController extends Controller
{

    public function test(){

    }
    public function balance(Request $request){
        $all_data = $request -> post();
        if(empty($all_data['merchant_number'])){
            return fails('请传商户号',1001);
        }
        if(empty($all_data['sign'])){
            return fails('请传签名',1001);
        }
        try{
            if(isSign($all_data)){
                $balance_info = XnMerchant::where('merchant_number',$all_data['merchant_number'])
                    -> select('merchant_number','balance','nickname','deduction_balance')
                    -> first()
                    -> toArray();
                if(empty($balance_info)){
                    return fails('未查询到该商户余额数据',1002);
                }
                $result = [
                    'business_number' =>$balance_info['merchant_number'],
                    'name' =>$balance_info['nickname'],
                    'balance' =>round($balance_info['balance'] - $balance_info['deduction_balance'],4)
                ];
                return successly($result);
            }else{
                return fails('签名错误',1005);
            }
        }catch (\Exception $e){
            return fails('未知错误',9999);
        }

    }

    public function goods(Request $request){
        $all_data = $request -> post();
        if(empty($all_data['merchant_number'])){
            return fails('请传商户号',1001);
        }
        if(empty($all_data['sign'])){
            return fails('请传签名',1001);
        }
        if(empty($all_data['goods_code'])){
            return fails('商品编码不得为空',1001);
        }
        try {
            if (isSign($all_data)) {
                $where[] = ['id', '=', $all_data['goods_code']];
                $where[] = ['release', '=', 1];
                $goods_info = XnSupplierGoods::where($where)
                    ->select('goods_name', 'id as product_id', 'price')
                    ->first()
                    ->toArray();
                if (empty($goods_info)) {
                    return fails('未查询到商品数据', 1002);
                }
                return successly($goods_info);
            } else {
                return fails('签名错误', 1005);
            }
        }catch (\Exception $e){
            return fails('未知错误',9999);
        }
    }

    public function order(Request $request){
        $all_data = $request -> post();
        if(empty($all_data['merchant_number'])){
            return fails('请传商户号',1001);
        }
        if(empty($all_data['sign'])){
            return fails('请传签名',1001);
        }
        if(empty($all_data['order_num'])){
            return fails('订单号不得为空',1001);
        }
        try{
            if(isSign($all_data)) {
                $where[]= ['order_sn','=',$all_data['order_num']];
                $where[]= ['merchant_number','=',$all_data['merchant_number']];
                $order_info = XnOrder::where($where)
                    -> select('order_sn as order_num','account','gid as product_id','price','goods_num as num','all_money as recharge_money','recharge_status as status','recharge_time as time','merchant_number as business_number')
                    -> first()
                    -> toArray();
                if(empty($order_info)){
                    return fails('未查询到订单数据',1002);
                }
                if($order_info['status'] == -1){
                    $order_info['msg'] = '充值中';
                }else if($order_info['status'] == 1){
                    $order_info['msg'] = '充值成功';
                }else if($order_info['status'] == 2){
                    $order_info['msg'] = '充值失败';
                }else if($order_info['status'] == 3){
                    $order_info['msg'] = '待充值';
                }
                return successly($order_info);
            }else{
                return fails('签名错误',1005);
            }
        }catch (\Exception $e){
            return fails('未知错误',9999);
        }
    }
}
